import 'package:dk_flutter_base/dk_core.dart';
import 'package:flutter/material.dart';

class LoadingDialog {
  DialogOption _dialogOption;
  GlobalKey _loadingDialogGlobalKey;
  LoadingDialogWidget _dialogWidget;

  LoadingDialog.create({DialogOption dialogOption}) {
    _dialogOption = dialogOption ?? DialogOption();
  }

  LoadingDialog show(BuildContext context) {
    if (null == _dialogWidget) {
      if (null == _loadingDialogGlobalKey) {
        _loadingDialogGlobalKey = new GlobalKey();
      }
      _dialogWidget =
          LoadingDialogWidget(_dialogOption, _loadingDialogGlobalKey);
    }
    showDialog(
        context: context,
        barrierDismissible: _dialogOption?.barrierDismissible,
        builder: (context) {
          return LoadingDialogWidget(_dialogOption, _loadingDialogGlobalKey);
        });
    return this;
  }

  void cancel() {
    if (null == _dialogWidget ||
        null == _loadingDialogGlobalKey?.currentWidget ||
        _loadingDialogGlobalKey.currentWidget.runtimeType !=
            _dialogWidget.runtimeType) {
      return;
    }
    RouteUtil.pop(_loadingDialogGlobalKey?.currentContext,
        popType: PopType.popOnly);
  }
}

class LoadingDialogWidget extends StatelessWidget {
  final DialogOption _dialogOption;

  LoadingDialogWidget(this._dialogOption, Key key) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Duration insetAnimationDuration = const Duration(milliseconds: 100);
    Curve insetAnimationCurve = Curves.decelerate;

    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets +
          EdgeInsets.symmetric(
              horizontal: _dialogOption?.insetHorizontalPadding,
              vertical: _dialogOption?.insetVerticalPadding),
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: SizedBox(
            width: _dialogOption?.width,
            height: _dialogOption?.height,
            child: Material(
              elevation: _dialogOption?.elevation,
              color: _dialogOption?.bgColor,
              type: _dialogOption?.bgType,
              //在这里修改成我们想要显示的widget就行了，外部的属性跟其他Dialog保持一致
              child: LogicPage.loadingPage(
                  loadingParam: _dialogOption.loadingParam ??
                      LogicParam.loading(loadingTxt: "")),
              shape: RoundedRectangleBorder(
                  borderRadius: _dialogOption?.shapeBorder ??
                      BorderRadius.all(
                          Radius.circular(_dialogOption?.bgRadius))),
            ),
          ),
        ),
      ),
    );
  }
}

class DialogOption {
  Widget dialogWidget;

  MaterialType bgType;
  Color bgColor;

  double width;
  double height;
  double insetHorizontalPadding;
  double insetVerticalPadding;

  double bgRadius;
  ShapeBorder shapeBorder;

  double elevation;

  Duration insetAnimationDuration;
  Curve insetAnimationCurve;

  bool barrierDismissible;

  Animation<Color> progressColor;
  LogicParam loadingParam;

  DialogOption(
      {this.dialogWidget,
      this.bgType = MaterialType.card,
      this.bgColor = Colors.white,
      this.width = 100,
      this.height = 100,
      this.insetHorizontalPadding = 40.0,
      this.insetVerticalPadding = 20.0,
      this.shapeBorder,
      this.bgRadius = 10,
      this.elevation = 0,
      this.insetAnimationDuration,
      this.insetAnimationCurve,
      this.barrierDismissible = true,
      this.progressColor,
      this.loadingParam});
}
